package cn.iotab.iotkit.server.demo.device.handler;

import cn.iotab.iotkit.transport.api.access.device.listener.DevicePropertyGetHandler;
import cn.iotab.iotkit.transport.api.access.model.custom.property.PropertyGetMessage;
import cn.iotab.iotkit.transport.api.access.model.custom.property.PropertyGetMessageReply;
import cn.iotab.iotkit.transport.api.access.model.message.DeviceMessageReply;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/*
 * 设备向平台getProperty，可用于设备向平台获取配置
 * 请求示例：
 * <pre>
 {
 *   "id": "123",
 *   "ver": "1.0",
 *   "ack":1,
 *   "type": "prop.get",
 *   "ts":1524448722000,
 *   "param": {
 *     "props": ["key1","key2"]
 *   }
 *  }
 *  </pre>
 * 响应示例:
 * {"code":200,"data":{"key1":"value_0","key2":"value_1"},"id":"123"}
 */
@Slf4j
@Component
public class MyDevicePropertyGetHandler extends DevicePropertyGetHandler {

    @Override
    protected ListenableFuture<DeviceMessageReply> doProcess(PropertyGetMessage message) {
        log.info("receive PropertyGetMessage: {}", message.toOriginMessage());
        //
        PropertyGetMessageReply reply = new PropertyGetMessageReply();
        reply.from(message);// copy productId 、deviceId、messageId
        int i = 0;
        for (String propKey : message.getProperties()) {
            reply.addProperty(propKey, "value_" + (i++));
        }
        reply.setSuccess();
        //
        return Futures.immediateFuture(reply);
    }
}
